<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Quiz/classes/PhpFox_ComponentTake.class.php, $Author:  $ */



/** "Take quiz" component
 * 
 * Parameters:
 *   1. oUser (required) - user's account object
 * 
 *   
 * @version $Id: PhpFox_ComponentTake.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.quiz
 */
class PhpFox_Mod_Quiz_ComponentTake extends BaseComponent
{
    function process()
    {
        
        $oSrvSec = &App::getModuleService('Account', 'Security');
        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */
        $oSrvSec->checkPerm('quiz', 'full');
        
        $oSrvQuiz = &$this->_oModule->getService('Quiz');
        /* @var $oSrvQuiz PhpFox_Mod_Quiz_ServiceQuiz */
        
        $oReq = &App::getRequest();
        /* @var $oReq Request */
        
        $iQuizId = $oReq->getInt('id');
        if (!$iQuizId)
        {
            App::gotoUrl('public.quiz');
        }
        
        $oQuiz = &$this->_oModule->getItem('Quiz');
        $oQuiz->load($iQuizId);
        if (!$oQuiz->aData['type'])
        {
            //quiz not approved yet
            App::gotoUrl('public.quiz');
        }
        
        
        $oAnswer = &$this->_oModule->getItem('Question');
        
        $aQuestions = $oSrvQuiz->getQuestions($iQuizId);
        //randomize answers
        foreach($aQuestions as $k=>$v)
        {
            for($i = 0; $i < 8; $i++)
            {
                $i1 = rand(1, 4);
                $i2 = rand(1, 4);
                list($aQuestions[$k]['a'.$i1], $aQuestions[$k]['a'.$i2]) = array($aQuestions[$k]['a'.$i2], $aQuestions[$k]['a'.$i1]);
            }
            if (!$aQuestions[$k]['a1'])
            {
                for($i2=2; $i2<=4; $i2++)
                {
                    if ($aQuestions[$k]['a'.$i2])
                    {
                        $aQuestions[$k]['a1'] = $aQuestions[$k]['a'.$i2];
                        $aQuestions[$k]['a'.$i2] = '';
                        break;
                    }
                }
            }
        }
        
        
        $oUser = $this->getParam('oUser');

        $aResults = array();
        if ($oSrvQuiz->isPassedQuiz($oUser, $iQuizId))
        {
            //if user passed quiz show results (for this user or passed with name parameters)
            $sViewUser = $oReq->get('name');
            if ($sViewUser)
            {
                $oSrvAcc  = &App::getModuleService('Account', 'Account');
                /* @var $oSrvAcc PhpFox_Mod_Account_ServiceAccount */
                $oTargetUser = $oSrvAcc->getByUsername($sViewUser);
            }
            else
            {
                $oTargetUser = $oUser;
            }
            $aResults = $oSrvQuiz->getUserResults($oQuiz, $oTargetUser);
        }
        switch($oReq->getAction())
        {
            case 'quiz_post_answers':
                if (!$aResults)
                {
                    if ($oSrvQuiz->addAnswers($oQuiz, $oReq->getArray('aAnswer'), $oUser))
                    {
                        $oUrl = App::getUrl();
                        App::forward($oUrl->getUrl());
                    }
                    else
                    {
                        $this->addErrors($oSrvQuiz->getErrors());
                    }
                }
                break;
        }

        $oTpl = &$this->_oModule->getTemplate();
        $oTpl->assign(array(
            'aResults'    => $aResults,
            'aQuiz'       => $oQuiz->aData,
            'aQuestions'  => $aQuestions,
            'sUserPicUrl' => App::getParam('user.pic.url'),
            'bIsOwner'    => ($oUser->aData['user'] == $oQuiz->aData['user']),
            'aErrors'     => $this->getErrors(),
        ));
        return $oTpl->fetch('Take.html');
    }
}
?>